<?
#####################################################
#  COPYRIGHT NOTICE
#####################################################
# In the following statements, the term "this 
# program" refers to all files associated with the
# execution and distribution of Adtrackz.
#
# This Program is Copyright 2004 Jonah Klimack.
# All Rights Reserved. 
#
# Selling the code for this program, modifying 
# and/or redistributing the code for this program
# in part or in whole over the Internet or in any 
# other medium is expressly forbidden. Violators
# will be prosecuted to the fullest extent of the law
# Copyright and header information may not be
# modified.
#
# This program is distributed "as is" and without 
# warranty of any kind, either express or implied.
# In no event shall the liability of Jonah Klimack 
# for any damages, losses and/or causes of 
# action exceed the total amount paid by the 
# user for this software.		
#####################################################
# DO NOT MODIFY ANYTHING IN THIS FILE
#####################################################

require "header.php";
require "submenu.php";

//CAMPAIGN INFO
$campaigndata = mysql_fetch_row(mysql_query("select * from adtrackz_campaigns where ID='$cID'"));

//purpose
if ($campaigndata[8] == "all")
	$purpose = "Clicks, Actions & Sales";
elseif ($campaigndata[8] == "clicks")
	$purpose = "Clicks Only";
elseif ($campaigndata[8] == "actions")
	$purpose =  "Clicks & Actions";
elseif ($campaigndata[8] == "sales")
	$purpose = "Clicks & Sales";
elseif ($campaigndata[8] == "affiliate")
	$purpose = "Clicks, Sales, Actions on Affiliate Websites";

//group
if (!$campaigndata[1])
	$group = "default";
else
{
	$groupdata = mysql_fetch_row(mysql_query("select groupname from adtrackz_groups where ID='$campaigndata[1]'"));
	$group = $groupdata[0];
}



?>
<!--TABLE HEADER-->

<table width="100%">
<tr>
	<td>
		<p class="small" align="left">Campaigns > <?=$campaigndata[2]?> > Manage Multiple SubCampaigns</p>
	</td>
	<td>
		<?load_submenu($cID,$campaigndata[2])?>
	</td>
</tr>
</table>


<p class="heading">
Manage Multiple SubCampaigns In <?=$campaigndata[2]?>
</p><p>
This is a list of all your subcampaigns in the <b><?=$campaigndata[2]?></b> campaign. You can edit, delete or reset the hits of some or all these subcampaigns. To make the necessary modifications, click on the action checkmark and make the change, then select the action you would like to perform below and click Save Changes.
</p>

<?


//if bottom pulldown was selected
if ($submit_bottom)
{

	if (!$checked_subcampaign && $confirm != "yes")
	{
		alert("no subcampaigns were selected");	
		require "footer.php";
		exit;
	}
	elseif ($action == "edit") //make changes
	{
		for ($i=0;$i<count($checked_subcampaign);$i++) //for each checked campaign...
		{
			for ($j=0;$j<count($subID);$j++) //...go through all subcampaigns listed and...
			{
				//...look for the checked subcampaign...
				if ($subID[$j] == $checked_subcampaign[$i]) //$j is is the subcampaign to change
				{
					//check variables
					if (strlen($adcode[$j]) > 100)
						error("Ad code $adcode[$j] must be less than 100 characters.");
					elseif (!$adcode[$j])
						error("Ad code $adcode[$j] is a required field.");
					elseif (strlen($url[$j]) > 255)
						error("Url $url[$j] must be less than 255 characters.");
					elseif (!$url[$j])
						error("Url is a required field for $adcode[$j].");
					elseif (!preg_match("/http:\/\//", $url[$j]))
						error("Url $url[$j] is not valid.");
					elseif (!preg_match("/\b\d+\b/", $cost[$j]))
						error("Cost $cost[$j] must be in numerical format.");
					else
					{
						if ($ppc[$j] == "yes")
							$ppc_save = 1;
						else
							$ppc_save = 0;

						mysql_query("update adtrackz_subcampaigns set adcode='$adcode[$j]', url='$url[$j]', cost='$cost[$j]', description='$description[$j]', ppc='$ppc_save' where ID='$subID[$j]'");		
						alert ("$adcode[$j] has been updated successfully.");
						unset ($ppc_save);
						break;			
					}
				}
			}
		}
	}	
	elseif ($action == "delete") //delete the campaign
	{
		if (!$confirm)
		{
			for ($i=0;$i<count($checked_subcampaign);$i++) //for each checked campaign...
			{
				for ($j=0;$j<count($subID);$j++) //...go through all subcampaigns listed and...
				{
					//...look for the checked subcampaign...
					if ($subID[$j] == $checked_subcampaign[$i]) //$j is is the subcampaign to change
					{
						//build list of subcampaigns to delete
						$subcampaigns_2delete_display .= $adcode[$j].", ";
						$subcampaigns_2delete_form .= "<input type=hidden name=subID[] value=".$subID[$j].">";
						break;			
					}
				}
			}

			alert ("<p>Are you sure you want to remove the following subcampaigns:<b>$subcampaigns_2delete_display</b>? All existing data for these subcampaigns will be permanently deleted.</p>");
			?>
			<br><br>
			<table align=center>
			<tr>
				<td>
					<form action="submasscampaign.php" method=post>
					<input type=submit name=submit_bottom value=" Yes ">
					<?=$subcampaigns_2delete_form?>
					<input type=hidden name=action value=delete>
					<input type=hidden name=confirm value=yes>
					<input type=hidden name=cID value=<?=$cID?>>
					</form>
				</td>
				<td>
					<form action="submasscampaign.php" method=post>
					<input type=hidden name=cID value=<?=$cID?>>
					<input type=submit name=cancel value=" No ">
					</form>
				</td>
			</tr></table>
			<?	
			require "footer.php";
			exit;
		}
		else
		{
			for ($i=0;$i<count($subID);$i++)
			{
				$sub_del_adcode = mysql_fetch_row(mysql_query("select adcode from adtrackz_subcampaigns where ID='$subID[$i]'"));
				mysql_query("delete from adtrackz_subcampaigns where ID='$subID[$i]'");
				mysql_query("delete from adtrackz_clicks where subID='$subID[$i]'");
				mysql_query("delete from adtrackz_actions where subID='$subID[$i]'");
				mysql_query("delete from adtrackz_sales where subID='$subID[$i]'");
				alert("$sub_del_adcode[0] has been deleted.");
			}
			require "footer.php";
			exit;
		}
	}
	elseif($action == "reset")
	{
		if (!$confirm)
		{
			for ($i=0;$i<count($checked_subcampaign);$i++) //for each checked campaign...
			{
				for ($j=0;$j<count($subID);$j++) //...go through all subcampaigns listed and...
				{
					//...look for the checked subcampaign...
					if ($subID[$j] == $checked_subcampaign[$i]) //$j is is the subcampaign to change
					{
						//build list of subcampaigns to delete
						$subcampaigns_2delete_display .= $adcode[$j].", ";
						$subcampaigns_2delete_form .= "<input type=hidden name=subID[] value=".$subID[$j].">";
						break;			
					}
				}
			}

			alert ("<p>Are you sure you want to remove the data from the following subcampaigns:<b>$subcampaigns_2delete_display</b>? All existing data for these subcampaigns will be permanently deleted.</p>");
			?>
			<br><br>
			<table align=center>
			<tr>
				<td>
					<form action="submasscampaign.php" method=post>
					<input type=submit name=submit_bottom value=" Yes ">
					<?=$subcampaigns_2delete_form?>
					<input type=hidden name=action value=reset>
					<input type=hidden name=confirm value=yes>
					<input type=hidden name=cID value=<?=$cID?>>
					</form>
				</td>
				<td>
					<form action="submasscampaign.php" method=post>
					<input type=hidden name=cID value=<?=$cID?>>
					<input type=submit name=cancel value=" No ">
					</form>
				</td>
			</tr></table>
			<?	
			require "footer.php";
			exit;
		}
		else
		{
			for ($i=0;$i<count($subID);$i++)
			{
				$sub_res_adcode = mysql_fetch_row(mysql_query("select adcode from adtrackz_subcampaigns where ID='$subID[$i]'"));
				mysql_query("delete from adtrackz_clicks where subID='$subID[$i]'");
				mysql_query("delete from adtrackz_actions where subID='$subID[$i]'");
				mysql_query("delete from adtrackz_sales where subID='$subID[$i]'");
				alert("$sub_res_adcode[0] has been reset.");
			}
			require "footer.php";
			exit;
		}
	}
}

?>
<p>
All subcampaigns share the same purpose and group as it's parent campaign.<br><br>

The group for these subcampaigns is: <b><?=$group?><br></b>
The purpose is: <b><?=$purpose?></b>
</p>
<br>
<?
if($from && !preg_match("/\b\d+\b/",$from))
{
	error("Starting from sale # must be an integer value.");
	$from = 0;
}
elseif (!$from)
	$from = 0;
else
	$from -= 1;
if($display_limit && !preg_match("/\b\d+\b/",$display_limit))
{
	error("Number of rows must be an integer value.");	
	$display_limit = 20;
}
if (!$display_limit)
	$display_limit = 20;

$subcampaigns = mysql_query("select * from adtrackz_subcampaigns where campaignID='$cID' order by adcode asc limit $from, $display_limit");
$from += 1;
?>

<form action="submasscampaign.php" method="post">
<input type="hidden" name=cID value=<?=$cID?>>
<table width=90% align=center>
<tr>
	<td>
		<input type="submit" name="submit_display" value="Show:"  >
		<input type="text" name=display_limit  size=3 maxlength=3 value=<?=$display_limit?>> rows 
		starting from subcampaign #<input type="text" name=from  size=3 maxlength=10 value=<?=$from?>>
	</td>
</tr>
</table>
<br>

<form action="submasscampaign.php" method=post>
<input type=hidden name="cID" value=<?=$cID?>>
<table width="580" border="1" cellspacing="0" cellpadding="5" align="center" bordercolor="#e0e0e0">
<tr>
	<td class="tbhead1">SubCampaign</td>
	<td class="tbhead1">Url</td>
	<td class="tbhead1">Cost</td>
	<td class="tbhead1">PPC</td>
	<td class="tbhead1">Description</td>
	<td class="tbhead1">Action</td>
</tr>
<?



//GET LIST OF SUBCAMPAIGNS

while ($subcampaigndata = mysql_fetch_row($subcampaigns))
{
	if ($subcampaigndata[6])
	{
		$ppc_yes = "selected";
	}
	else
		$ppc_no = "selected";
	?>
	<tr>
		<td class="tb1">
			<input type=hidden name="subID[]" value=<?=$subcampaigndata[0]?>>
			<input type=input name="adcode[]" size=10 value=<?=$subcampaigndata[2]?>>
		</td>
		<td class="tb1"><input type=input name="url[]" value=<?=$subcampaigndata[3]?>></td>	
		<td class="tb1"><input type=input name="cost[]" size=3 value=<?=$subcampaigndata[4]?>></td>	
		<td class="tb1">
			<select name="ppc[]">
			<option value="yes" <?=$ppc_yes?>>Yes</option>
			<option value="no" <?=$ppc_no?>>No</option>
			</select> 
		</td>	
		<td class="tb1"><textarea name="description[]" cols=12 rows=2><?=$subcampaigndata[5]?></textarea>		
		<td class="tb1"><input type=checkbox name="checked_subcampaign[]" value="<?=$subcampaigndata[0]?>"></td>
	</tr>
	<?
	unset ($ppc_yes, $ppc_no);
}
?>
</table>
<br><br>
<center>
With Selected Campaigns: &nbsp;
<select name="action">
<option value="edit">Edit SubCampaigns</option>
<option value="reset">Reset Hits</option>
<option value="delete">Delete SubCampaigns</option>
</select>
<input type=submit name="submit_bottom" value="Make Changes">
</center>

</form>
<?

require "footer.php";
?>